/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 29, 2011 1:12:20 PM
 */
package carrello.impl.boundary.administrator;

import carrello.impl.boundary.AbstractUI;
import carrello.impl.controller.administrator.CategoryAssociationCTRL;
import carrello.model.association.CategoryAssociation;
import carrello.model.entities.Category;
import carrello.model.entitydao.CategoryDAO;
import java.util.List;

public class CategoryAssociationUI extends AbstractUI{
    private String source;
    private String destination;
    private String name;
    private List<Category> catList;
    private String error;

    public List<Category> getCatList() {
        return new CategoryDAO().getAll();
    }

    public void setCatList(List<Category> catList) {
        this.catList = catList;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }
    
    
    public String execute()
    {
        if (name == null ) {
            return INPUT;
        } else if (source.equals(destination)) {
            setError("Source and destination can not have the same name");
            return ERROR;
        } else if(name.trim().equals("")) {
            setError("Name cannot be left blank");
            return ERROR;
        } // if
        addAssociation();
        return SUCCESS;
    } // execute
    public void addAssociation()
    {
        CategoryAssociationCTRL ca_ctrl = new CategoryAssociationCTRL();
        int source_id = new CategoryDAO().getId(source);
        int dest_id = new CategoryDAO().getId(destination);
        ca_ctrl.addAssociation(source_id, dest_id, name);
    } // addAssociation
}
